<template>
  <div>
    <!-- 导航 -->
    <van-nav-bar
      title="会员登录"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />

    <!-- 登录表单 -->
    <div class="container">
      <div class="title">
        <h3>手机号登录</h3>
        <p>未注册的手机号登录后将自动注册</p>
      </div>

      <div class="form">
        <div class="form-item">
          <input
            class="inp"
            maxlength="11"
            placeholder="请输入手机号码"
            type="text"
            v-model="phone"
          />
        </div>
        <div class="form-item">
          <input
            class="inp"
            maxlength="5"
            placeholder="请输入图形验证码"
            type="text"
            v-model="yzm"
          />
          <img v-if="base64" :src="base64" alt="" @click="getPicCode" />
        </div>
        <div class="form-item">
          <input
            class="inp"
            placeholder="请输入短信验证码"
            type="text"
            v-model="dxyzm"
          />
          <button @click="getCode">
            {{
              totalTime === currentTime
                ? "获取验证码"
                : currentTime + `秒后重新发送`
            }}
          </button>
        </div>
      </div>

      <div class="login-btn" @click="login">登录</div>
    </div>
  </div>
</template>

<script>
import { getPicCode, sendCode, login } from "@/api/login";
export default {
  name: "login-index",
  data() {
    return {
      phone: "",
      yzm: "",
      dxyzm: "",
      base64: "",
      key: "",
      md5: "",
      totalTime: 5,
      currentTime: 5,
      timer: null,
    };
  },
  created() {
    this.getPicCode();
  },
  destroyed() {
    // 销毁时清除定时器，烧毁页面不会自动清除定时器
    clearInterval(this.timer);
  },
  methods: {
    onClickLeft() {
      this.$router.back(-1);
    },
    async getPicCode() {
      const res = await getPicCode();
      this.base64 = res.data.base64;
      this.key = res.data.key;
      this.md5 = res.data.md5;
    },
    async getCode() {
      // 校验表单
      if (!this.validatedSend()) {
        return;
      }

      // 发送验证码
      const res = await sendCode(this.yzm, this.key, this.phone);
      this.$toast(res.message);

      // 倒计时
      if (this.timer !== null) {
        return;
      }
      this.timer = setInterval(() => {
        this.currentTime--;
        if (this.currentTime < 1) {
          this.currentTime = this.totalTime;
          clearInterval(this.timer);
          this.timer = null;
        }
      }, 1000);
    },
    // 登录
    async login() {
      if (!this.validatedLogin()) {
        return;
      }
      const res = await login(this.phone, this.dxyzm);
      this.$store.commit("user/setUserInfo", res.data);
      this.$router.push("/");
      this.$toast(res.message);
    },
    // 发送验证码验证表单
    validatedSend() {
      if (!/^1[3-9]\d{9}$/.test(this.phone)) {
        this.$toast("手机号错误");
        return false;
      }
      if (!/^\w{4}$/.test(this.yzm)) {
        this.$toast("图形验证码错误");
        return false;
      }
      return true;
    },
    validatedLogin() {
      if (!this.validatedSend()) {
        return false;
      }
      if (!/^\d{6}$/.test(this.dxyzm)) {
        this.$toast("短信验证码错误");
        return false;
      }
      return true;
    },
  },
};
</script>

<style lang="less" scoped>
.container {
  padding: 49px 29px;

  .title {
    margin-bottom: 20px;
    h3 {
      font-size: 26px;
      font-weight: normal;
    }
    p {
      line-height: 40px;
      font-size: 14px;
      color: #b8b8b8;
    }
  }

  .form-item {
    border-bottom: 1px solid #f3f1f2;
    padding: 8px;
    margin-bottom: 14px;
    display: flex;
    align-items: center;
    .inp {
      display: block;
      border: none;
      outline: none;
      height: 32px;
      font-size: 14px;
      flex: 1;
    }
    img {
      width: 94px;
      height: 31px;
    }
    button {
      height: 31px;
      border: none;
      font-size: 13px;
      color: #cea26a;
      background-color: transparent;
      padding-right: 9px;
    }
  }

  .login-btn {
    width: 100%;
    height: 42px;
    margin-top: 39px;
    background: linear-gradient(90deg, #ecb53c, #ff9211);
    color: #fff;
    border-radius: 39px;
    box-shadow: 0 10px 20px 0 rgba(0, 0, 0, 0.1);
    letter-spacing: 2px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>
